package PlotPourri.Database;

import PlotPourri.BandName;
import PlotPourri.BandName;
import PlotPourri.Observation;
import PlotPourri.Observation;
import PlotPourri.Star;
import PlotPourri.Star;
import PlotPourri.Database.StarDatabase;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class DatabaseLMCii extends StarDatabase {

    public DatabaseLMCii(File path) {
        super(path);
        Map<Integer, Star> stars = new HashMap<Integer, Star>();
        Scanner sc = null;
        Scanner ext = null;
        try {
            sc = new Scanner(path);
        } catch (FileNotFoundException ex) {
            System.err.print(ex + "\n");
        }
        //Datfile RADeclExt = new Datfile(new File("/home/earl/Documents/Research/Astrophysics/LMC/TypeII/6.dat"));
        Observation RADeclExt = new Observation(new File("E:\\Research\\Astrophysics\\LMC\\TypeII\\6.dat"));
        int counter = 0;
        if (sc != null) {
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                if (line.startsWith("#")) continue;
                Star star = null;
                int name = Integer.parseInt(line.split("\\s+")[2]);
                if (stars.containsKey(name)) star = stars.get(name);
                else star = new Star(BandName.getVI(), name);
                star.setType((line.split("\\s+")[5].equalsIgnoreCase("BLHer")) ? Star.Type.BLHer : Star.Type.WVir);
                star.setPeriod(Double.parseDouble(line.split("\\s+")[8]));
                double Av = RADeclExt.get(counter++)[2];
                for (BandName bandName : BandName.getVI()) {
                    String full = path.getPath().split("query")[0] + bandName + File.separatorChar + line.split("\\s+")[0] + ".dat";
                    Observation datfile = new Observation(full, Integer.parseInt(full.split("-")[full.split("-").length - 1].split(".dat")[0]));
                    datfile.setPhasePosition(-1);
                    datfile.setMagnitudePosition(1);
                    datfile.setErrorPosition(2);
                    datfile.setJulianDatePosition(0);
                    datfile.setPeriod(star.getPeriod());
                    if (Av == 0) Av = 0.324;
                    if (bandName.equals(BandName.V)) datfile.setReddening(Av);
                    else datfile.setReddening((1.96 / 3.24) * Av);
                    star.getBand(bandName).add(datfile);
                }
                if (!stars.containsKey(name)) put(star.getName(), star);
            }
            sc.close();
        }
    }
}
